package org.wisdom.ab.domain.company.repository;

import java.util.List;

import org.springframework.data.domain.*;
import org.wisdom.ab.domain.company.entity.AbCompany;
import org.wisdom.data.jpa.repository.WisdomRepository;

/**
 * 公司仓储
 *
 * @author 张智研
 * @date 2022/05/16 09:54
 **/
public interface CompanyRepository extends WisdomRepository<AbCompany, String> {

    /**
     * 查询公司列表
     *
     * @author 张智研
     * @date 2022/05/20 15:02
     * @param company
     *            公司条件
     * @param page
     *            页码
     * @param size
     *            大小
     * @param sort
     *            排序
     * @return org.springframework.data.domain.Page<org.wisdom.ab.domain.entity.AbCompany>
     */
    default Page<AbCompany> findAll(AbCompany company, int page, int size, Sort sort) {
        ExampleMatcher matcher = ExampleMatcher.matching()
            .withMatcher("companyCode", ExampleMatcher.GenericPropertyMatcher::startsWith)
            .withMatcher("companyName", ExampleMatcher.GenericPropertyMatchers.contains())
            .withMatcher("companyNameEn", ExampleMatcher.GenericPropertyMatchers.contains())
            .withMatcher("companyDesc", ExampleMatcher.GenericPropertyMatchers.contains()).withIgnorePaths("password");
        Example<AbCompany> companyExample = Example.of(company, matcher);
        return findAll(companyExample, PageRequest.of(page, size, sort));
    }

    /**
     * 查询公司列表
     *
     * @author 张智研
     * @date 2022/05/20 15:02
     * @param company
     *            公司条件
     * @param page
     *            页码
     * @param size
     *            大小
     * @return org.springframework.data.domain.Page<org.wisdom.ab.domain.entity.AbCompany>
     */
    default Page<AbCompany> findAll(AbCompany company, int page, int size) {
        return findAll(company, page, size, Sort.unsorted());
    }

    /**
     * 查询公司列表
     *
     * @author 张智研
     * @date 2022/05/20 15:02
     * @param company
     *            公司条件
     * @return org.springframework.data.domain.Page<org.wisdom.ab.domain.entity.AbCompany>
     */
    default List<AbCompany> findAll(AbCompany company) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("companyCode", ExampleMatcher.GenericPropertyMatcher::startsWith)
                .withMatcher("companyName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("companyNameEn", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("companyDesc", ExampleMatcher.GenericPropertyMatchers.contains()).withIgnorePaths("password");
        Example<AbCompany> companyExample = Example.of(company, matcher);
        return findAll(companyExample);
    }
}
